/**
 * 
 */
package com.gmail.hh.flickrj.sample.android.images;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Gallery.LayoutParams;

import com.gmail.hh.flickrj.sample.android.R;
import com.gmail.hh.flickrj.sample.android.data.PhotoData;
import com.gmail.hh.flickrj.sample.android.data.PhotolistData;
import com.gmail.hh.flickrj.sample.android.images.ImageUtils.DownloadedDrawable;
import com.gmail.hh.flickrj.sample.android.tasks.LoadImageTask;
import com.gmail.hh.flickrj.sample.android.tasks.LoadImageTask.IImageLoadedListener;
import com.googlecode.flickrjandroid.photos.Photo;

/**
 * @author Toby Yu(yu yang..226@gmail.com)
 *
 */
public class LazyAdapterPhotolist extends BaseAdapter implements
												IImageLoadedListener{

	private static final Logger logger = LoggerFactory.getLogger(LazyAdapterPhotolist.class);
	
	public static final int LAYOUT_RESOURCE_LIST 		= R.layout.photolist_row;
	public static final int LAYOUT_RESOURCE_GRID 		= R.layout.photogrid_box;
	public static final int LAYOUT_RESOURCE_GALLERY3 	= -1;
	public static final int PHOTO_SIZE_SMALLSQUARE	 	=  1;
	public static final int PHOTO_SIZE_MEDIUM		 	=  2;
	public static final int PHOTO_SIZE_LARGE		 	=  3;
	
    private Activity activity;
    private PhotolistData photolistData;
    private static LayoutInflater inflater=null;
    private Integer origHeight = null;
    private Integer origWidth  = null;
    private int layoutResource  = 0;
    private int photoSize  = 0;
    
    public LazyAdapterPhotolist(Activity a, PhotolistData d) {
        activity = a;
        photolistData = d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
	@Override
    public int getCount() {
        return photolistData.size();
    }

	@Override
	   public PhotoData getItem(int position) {
		return photolistData.getAtPosition(position);
    }

	@Override
	   public long getItemId(int position) {
        return position;
    }
    
	@Override
	   public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        PhotoData photoData = photolistData.getAtPosition(position);
        Photo photo = photoData.getPhoto();
		if (layoutResource==0) {
       	 	layoutResource = LAYOUT_RESOURCE_LIST; // default
        }
        String photoUrl = photo.getSmallSquareUrl();  // default
		if (photoSize== PHOTO_SIZE_MEDIUM) {
			photoUrl = photo.getMediumUrl(); 
        }
		if (photoSize== PHOTO_SIZE_LARGE) {
			photoUrl = photo.getLargeUrl(); 
        }

		if (layoutResource==LAYOUT_RESOURCE_LIST || layoutResource==LAYOUT_RESOURCE_GRID) {
        	vi =null; // when reusing view there is a problem with multiple use f imageView
	        if(vi == null)  {
	            vi = inflater.inflate(layoutResource, null);
	        }
	        
	        TextView title=(TextView)vi.findViewById(R.id.imageTitle);;
	        ImageView imageView=(ImageView)vi.findViewById(R.id.imageIcon);
	        if (origHeight == null && origWidth==null) {
	        	 origHeight = Integer.valueOf( imageView.getLayoutParams().height );
	        	 origWidth  = Integer.valueOf( imageView.getLayoutParams().width );
	        }
	        float zoomFactor = photolistData.getPhotolistZoomFactor();
	        int newHeight =  (int) (origHeight.intValue() * zoomFactor);
	        int newWidth  =  (int) (origWidth.intValue()  * zoomFactor);
	        imageView.getLayoutParams().height = newHeight;
	        imageView.getLayoutParams().width  = newWidth;
	        String titleShort = photo.getTitle();
			if (layoutResource==LAYOUT_RESOURCE_GRID && titleShort!=null && titleShort.length()>20 ) {
				titleShort = titleShort.substring(0, 18) + "...";
	        }
	        title.setText(titleShort);
	        if (imageView != null && photoUrl!=null) {
	        	photoData.setImageViewSmallSquare(imageView);
	        	boolean showProgress = false;
	        	long delayMSEC = 0;
	        	LoadImageTask task = new LoadImageTask(this, delayMSEC, photoData, photoData.getImageViewSmallSquare(), showProgress, null);
	            Drawable drawable = new DownloadedDrawable(task);
	            imageView.setImageDrawable(drawable);
	            task.execute( photoUrl );
	        }
	        if (layoutResource==LAYOUT_RESOURCE_LIST  ) {
		        ImageView viewIcon = (ImageView)vi.findViewById(R.id.viewIcon);
		        if (photo.getViews() >= 0) {
		        	viewIcon.setImageResource(R.drawable.views);
		        	TextView viewsText = (TextView)vi.findViewById(R.id.viewsText);
		        	viewsText.setText(String.valueOf(photo.getViews()));
		        } else {
		        	viewIcon.setImageBitmap(null);
		        }
	        }
        }

		if (layoutResource==LAYOUT_RESOURCE_GALLERY3) {
	        ImageView imageView = new ImageView(activity);
	        imageView.setAdjustViewBounds(true);
	        imageView.setLayoutParams(new Gallery.LayoutParams(
	                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
	        imageView.getLayoutParams().height = 150;  // unit is dp (smallSquare is 75x75dp)
	        imageView.getLayoutParams().width  = 150;
	 //       i.setBackgroundResource(R.drawable.picture_frame);
	        if ( photoUrl!=null) {
	        	boolean showProgress = false;
	        	LoadImageTask task = new LoadImageTask( imageView, showProgress, null);
	            Drawable drawable = new DownloadedDrawable(task);
	            imageView.setImageDrawable(drawable);
	            task.execute( photoUrl );
	            vi=imageView;
	        }
        }
		
        return vi;
    }

	// callback method after loadImage asynchron
	@Override
	public void onImageLoaded(PhotoData photoData) {
		// no action necessary nothing after loading image
	}
	

	public void setLayoutResource(int layoutResource) {
		this.layoutResource = layoutResource;
	}
	
	public void setPhotoSize(int photoSize) {
		this.photoSize = photoSize;
	}

	
}
